package com.jzg.jcptadmin.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.MD5Utils;
import com.jzg.jcptadmin.app.ActivityHelp;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.app.AppManager;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.data.local.ACache;
import com.jzg.jcptadmin.data.vo.User;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.pedant.SweetAlert.SweetAlertDialog;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 系统设置界面
 * SystemSetActivity
 *
 * @author sunbl
 *         created at 2015/12/29 11:19
 */
public class SystemSetActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.title_content)
    TextView titleContent;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_set);
        ButterKnife.bind(this);
        titleContent.setText("系统设置");
        user = appContext.getUser();
    }

    @OnClick({R.id.changepd, R.id.tv_edit})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.changepd://账户详情
                ActivityHelp.startActivity(this, UserInfoActivity.class);
                break;
            case R.id.tv_edit:
                new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("提示")
                        .setContentText("您确定要退出该账户吗？")
                        .setConfirmText("是的").setCancelText("取消")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sDialog) {

                                AppContext.httpService.login(getLoginParams())
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .subscribeOn(Schedulers.io()).subscribe(new Observer<User>() {
                                    @Override
                                    public void onCompleted() {
                                        dismissDialog();
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        logout();
                                    }

                                    @Override
                                    public void onNext(User u) {
                                        logout();
                                    }
                                });
                                sDialog.dismissWithAnimation();
                                showDialog();
                            }
                        }).show();

                break;
            default:
                break;
        }

    }

    public void logout() {
        Set<String> tagSet = new HashSet<>();
        JPushInterface.setTags(appContext, tagSet, null);
        //清空缓存的数据
        ACache.get(appContext).remove("imagecache");
        ACache.get(appContext).remove("videopath");
        ACache.get(appContext).remove("taskInfo");
//        if (user.getIsDL().equals("1")) {
//            AppManager.getAppManager().finishActivity(YiXinHomeActivity.class);
//        } else {
//
//        }
        AppManager.getAppManager().finishActivity(HomeActivity.class);
        Intent intent = new Intent(SystemSetActivity.this, MainActivity.class);
        intent.putExtra("edit", false);
        startActivity(intent);
        finish();
    }

    public Map<String, String> getLoginParams() {
        Map<String, String> params = new HashMap<>();
        params.put("op", "logout");
        params.put("tokenid", "6");
        params.put("userid", String.valueOf(user.getUserId()));
        if (!TextUtils.isEmpty(JPushInterface.getRegistrationID(appContext))) {
            params.put("registrationId", JPushInterface.getRegistrationID(appContext));
        }
        Map<String, Object> signMap = new HashMap<>();
        signMap.putAll(params);
        params.put("sign", MD5Utils.getMD5Sign(signMap));
        return params;
    }
}
